﻿//
// Copyright © Massxess BV This file is part of MSRP-CSharp.net Stack.
// 
// MSRP-CSharp.net Stack is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation, version 3 or later.
// 
// MSRP-CSharp.net Stack is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
// for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with MSRP-CSharp.net Stack. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSRP.Java;

namespace MSRP.Utils
{
    /// <summary>
    /// 
    /// </summary>
    public class TextUtils
    {
        /// <summary>
        /// 
        /// </summary>
        //public static Charset usascii = Charset.forName("US-ASCII");

        public static Random randomGenerator = new Random();

        /// <summary>
        /// Generates a number of random alpha-numeric characters in US-ASCII
        /// </summary>
        /// <param name="byteArray">the byte array that will contain the newly generated
        ///                         bytes. the number of generated bytes is given by the length of
        ///                         the byteArray</param>
        public static void generateRandom(byte[] byteArray)
        {

            int i;
            randomGenerator.NextBytes(byteArray);
            for (i = 0; i < byteArray.Length; i++)
            {
                if (byteArray[i] < 0)
                    byteArray[i] *= 0; //-1

                while (!((byteArray[i] >= 65 && byteArray[i] <= 90)
                    || (byteArray[i] >= 97 && byteArray[i] <= 122) || (byteArray[i] <= 57 && byteArray[i] >= 48)))
                {

                    if (byteArray[i] > 122)
                        byteArray[i] -= Convert.ToByte(randomGenerator.Next(Convert.ToInt32(byteArray[i])));
                    if (byteArray[i] < 48)
                        byteArray[i] += Convert.ToByte(randomGenerator.Next(5));
                    else
                        byteArray[i] += Convert.ToByte(randomGenerator.Next(10));
                }
            }
        }

        // to hex converter
        private static char[] toHex = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

        /// <summary>
        /// convert an array of bytes to a hexadecimal string
        /// </summary>
        /// <param name="b">byte-array to convert to the hexadecimal string</param>
        /// <returns>the string</returns>
        public static string toHexString(byte[] b) 
        {
            int pos = 0;
            char[] c = new char[b.Length * 2];

            for (int i = 0; i <  b.Length; i++) 
            {
                c[pos++] = toHex[(b[i] >> 4) & 0x0F];
                c[pos++] = toHex[b[i] & 0x0f];
            }

            return c.GetString();
        }
    }
}
